package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel>
        implements AdChannelService {

    //分页查询频道列表
    @Override
    public ResponseResult findByNameAndPage(ChannelDTO dto) {
        //检查参数
        dto.checkParam();
        //分页查询
        IPage<AdChannel> page = new Page<>(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<AdChannel> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(dto.getName())){
            queryWrapper.like(AdChannel::getName,dto.getName());
        }
        queryWrapper.orderByAsc(AdChannel::getOrd);

        IPage<AdChannel> channelIPage = this.page(page, queryWrapper);
        //封装结果
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), channelIPage.getTotal());
        responseResult.setData(channelIPage.getRecords());
        return responseResult;
    }

    /**新增频道
     * 频道名称不能为空, 不能大于10个字符
     * 且频道名称不可以重复
     * @param channel
     * @return
     */
    @Override
    public ResponseResult insert(AdChannel channel) {
        //参数验证
        if (channel==null||StringUtils.isBlank(channel.getName())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //判断频道是否存在
        LambdaQueryWrapper<AdChannel> wrapper = new LambdaQueryWrapper<>();
        int count = this.count(wrapper.eq(AdChannel::getName, channel.getName()));
        if (count>0){
            //存在
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"该频道已存在");
        }
        //添加数据
        channel.setCreatedTime(new Date());
        this.save(channel);
        //返回结果
        return ResponseResult.okResult();
    }

    /**
     * 修改频道
     *如果name无值不用处理 （因为启用禁用不用传name）
     *如果name有值，需要检查是否改变 是否重复  长度是否合格
     * @param channel
     * @return
     */
    @Override
    public ResponseResult updatead(AdChannel channel) {
        //参数验证
        if (channel == null || channel.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断修改信息是否有效
        AdChannel adChannel = this.getById(channel.getId());
        if (adChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"频道信息不存在");
        }
        //校验名称唯一
        LambdaQueryWrapper<AdChannel> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(channel.getName())&&!channel.equals(adChannel.getName())){
            int count = this.count(wrapper.eq(AdChannel::getName, channel.getName()));
            if (count>0){
                //存在
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"该频道已存在");
            }
        }
        //修改数据
        this.updateById(channel);
        //返回结果
        return ResponseResult.okResult();
    }

    /**
     * 根据id删除频道
     * 如果当前状态为有效则不能删除
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteById(Integer id) {
        //参数验证
        if (id==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断id是否有效
        AdChannel adChannel = this.getById(id);
        if (adChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //判断是否启用
        if (adChannel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //删除频道
        this.deleteById(id);
        return ResponseResult.okResult();
    }
}
